// pages/order/index.js
const app = getApp()
const wxpay = require('../../utils/pay.js')
const dateUtil = require('../../utils/date.js')
const CONFIG = require('../../config.js')
const WXAPI = require('../../wxapi/main.js')
import Toast from 'tdesign-miniprogram/toast/index';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    vipDetail: {},
    userInfo: {},
    orderMethodMap: {
      1: "现金",
      2: "用券抵扣",
      3: "现金+券"
    },
    paymentVenueMap: {
      "1": "开通会员",
      "2": "预约",
      "3": "机位结算"
    },
    vipBenefits: [],
    interval: null,
  },
  queryVIPBenefits() {


    WXAPI.queryVipBenefitsBuyVipId(this.data.vipDetail.id).then(res => {
      var vipBenefits = res;
      // vipBenefits.forEach(item=>{
      //   item.pic="../../images/168e05bf09aa795bb778728f5afbf38.png"
      // })
      this.setData({
        vipBenefits
      });
      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },

  onPullDownRefresh() {
    this.queryOrder(this.data.payStatus, () => {
      this.setData({
        'baseRefresh.value': false
      })
    });

  },
  onScroll(e) {
    const { scrollTop } = e.detail;

    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },
  onTabsClick(e) {
    var payStatus = e.detail.value;
    this.setData({
      payStatus
    })
    this.queryOrder(payStatus);
  },

  toPay(e) {

    var payment_method=1
    var data=this.data.vipDetail;
    var {userInfo}=this.data;

   if(userInfo.recharge_balance && userInfo.recharge_balance>=data.price)
     payment_method=4

    WXAPI.buyVip(this.data.vipDetail.id, {
      user_id: this.data.userInfo.id,
      payment_method
    }).then(res => {

      if (res.code == 200) {
        var payment = res.data;

        wx.requestPayment({
          appid: payment.appid,
          timeStamp: payment.timeStamp,
          nonceStr: payment.nonceStr,
          package: payment.package,
          signType: payment.signType,
          paySign: payment.paySign,
          success: (res) => {
            wx.showToast({
              title: '订购成功',
            })
            wx.switchTab({
              url: "/pages/my/index"
            })
          },
          fail: (res) => {
            console.log(res)
            wx.showToast({
              icon:"none",
              "title": "订购取消"
            });
            clearInterval(this.data.interval)
          }
        });

        var interval = setInterval(() => {
          WXAPI.queryOrderByNo(payment.order_no).then(res => {
            if (res.status == 2) {
              clearInterval(this.data.interval)
              wx.switchTab({
                url: "/pages/my/index"
              })



            }
          })
        }, 100)

        this.setData({
          interval
        })

      } else {
        Toast({
          context: this,
          selector: '#t-toast',
          message: "会员订购失败:" + (res.message || res.msg),
        });
      }

      wx.hideNavigationBarLoading();
    }).catch((e) => {
      wx.hideNavigationBarLoading();
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    this.setData({
      vipDetail: JSON.parse(e.data),
      userInfo: wx.getStorageSync("userInfo")
    })
    this.queryVIPBenefits();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const systemInfo = wx.getSystemInfoSync();
    //屏幕高，屏幕宽，可视区高度，状态栏高，像素比
    var { screenHeight, screenWidth, windowHeight, statusBarHeight, pixelRatio } = systemInfo;
    var margin = screenWidth * 20 / 750;
    var itemWidth = (screenWidth - 3 * margin) / 2;
    var swiperHeight = 475 * screenWidth / 750;
    const navBarHeight = 44; // 顶部导航栏高度
    const tabBarHeight = 50; // 底部导航栏高度，注意这个值是固定的
    /**
     * 当我们自定义custiom的时候，底部导航栏不在可视区里面
     * https://blog.csdn.net/qq_46199553/article/details/126030693
     */
    const contentHeight = windowHeight - navBarHeight - statusBarHeight;
    var vip_title_top = navBarHeight + statusBarHeight + 10 * screenWidth / 750
    var vip_title_left = (screenWidth - (300 * screenWidth / 750)) / 2
    var vip_desc_title_left = (screenWidth - (400 * screenWidth / 750)) / 2
    var qy_item_width = screenWidth - 20 * 2 * screenWidth / 750
    var vip_desc_center_left = (screenWidth - (520 * screenWidth / 750)) / 2
    var vip_center_left = (screenWidth - (420 * screenWidth / 750)) / 2
    this.setData({
      vip_title_top,
      vip_title_left,
      vip_desc_title_left,
      vip_desc_center_left,
      vip_center_left,
      qy_item_width,
      userInfo: wx.getStorageSync("userInfo")

    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})